package com.ruoyi.system.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

/**
 * 用户表 数据层
 * 
 * @author ruoyi
 */
public interface SysUserMapper
{
    /**
     * 根据条件分页查询用户列表
     * 
     * @param sysUser 用户信息
     * @return 用户信息集合信息
     */
    public List<SysUser> selectUserList(SysUser sysUser);

    /**
     * 通过用户名查询用户
     * 
     * @param userName 用户名
     * @return 用户对象信息
     */
    public SysUser selectUserByUserName(String userName);

    /**
     * 通过用户ID查询用户
     * 
     * @param userId 用户ID
     * @return 用户对象信息
     */
    public SysUser selectUserById(Long userId);

    /**
     * 新增用户信息
     * 
     * @param user 用户信息
     * @return 结果
     */
    public int insertUser(SysUser user);

    /**
     * 修改用户信息
     * 
     * @param user 用户信息
     * @return 结果
     */
    public int updateUser(SysUser user);

    /**
     * 修改用户头像
     * 
     * @param userName 用户名
     * @param avatar 头像地址
     * @return 结果
     */
    public int updateUserAvatar(@Param("userName") String userName, @Param("avatar") String avatar);

    /**
     * 重置用户密码
     * 
     * @param userName 用户名
     * @param password 密码
     * @return 结果
     */
    public int resetUserPwd(@Param("userName") String userName, @Param("password") String password);

    /**
     * 通过用户ID删除用户
     * 
     * @param userId 用户ID
     * @return 结果
     */
    public int deleteUserById(Long userId);

    /**
     * 批量删除用户信息
     * 
     * @param userIds 需要删除的用户ID
     * @return 结果
     */
    public int deleteUserByIds(Long[] userIds);

    /**
     * 校验用户名称是否唯一
     * 
     * @param userName 用户名称
     * @return 结果
     */
    public int checkUserNameUnique(String userName);

    /**
     * 校验手机号码是否唯一
     *
     * @param phonenumber 手机号码
     * @return 结果
     */
    public SysUser checkPhoneUnique(String phonenumber);

    /**
     * 校验email是否唯一
     *
     * @param email 用户邮箱
     * @return 结果
     */
    public SysUser checkEmailUnique(String email);


    @Select("<script>" +
            "SELECT\n" +
            "\tu.*\n" +
            "FROM\n" +
            "\tsys_user u\n" +
            "LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id\n" +
            "LEFT JOIN sys_role r ON r.role_id = ur.role_id\n" +
            "WHERE 1=1 \n" +
            "<if test=\"roleKey!=null \">" +
            "AND r.role_key = #{roleKey} \n" +
            "</if>" +
            "<if test=\"userName!=null \">" +
            "AND u.user_name = #{userName} \n" +
            "</if>" +
            "<if test=\"nickName!=null \">" +
            "AND u.nick_name = #{nickName} \n" +
            "</if>" +
            "<if test='createTimeStart !=null and createTimeEnd!=null '> AND create_time  between #{createTimeStart} and #{createTimeEnd}" +
            "</if> " +
            "<if test=\"tenantCode!=null \">" +
            "AND u.tenant_code = #{tenantCode} " +
            "AND r.tenant_code = #{tenantCode} " +
            "</if>" +
            "</script>"
    )
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "dept_id", property = "deptId"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "nick_name", property = "nickName"),
            @Result(column = "user_type", property = "userType"),
            @Result(column = "del_flag", property = "delFlag"),
            @Result(column = "login_ip", property = "loginIp"),
            @Result(column = "login_date", property = "loginDate"),
            @Result(column = "create_by", property = "createBy"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_by", property = "updateBy"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "org_id", property = "orgId"),
            @Result(column = "station_id", property = "stationId"),
            @Result(column = "tenant_code", property = "tenantCode")
    })
    List<SysUser> listByRoleKeyAndTenantCode(@Param("roleKey") String roleKey,
                                             @Param("tenantCode") String tenantCode,
                                             @Param("userName") String userName,
                                             @Param("nickName") String nickName,
                                             @Param("createTimeStart") String createTimeStart,
                                             @Param("createTimeEnd") String createTimeEnd
    );

    int deleteUserByTenantCodes(List<String> tenantCodeList);


    List<Long> selectUserByTenantCodes(List<String> tenantCodeList);


    int deleteUserDatabaseByIds(Long[] userIds);
}
